package com.example.droolstest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.droolstest.dao.BusinessPropertyDao;
import com.example.droolstest.entity.BusinessProperty;
import com.example.droolstest.exception.ServiceException;
import com.example.droolstest.service.BusinessPropertyService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class BusinessPropertyServiceImpl extends ServiceImpl<BusinessPropertyDao, BusinessProperty> implements BusinessPropertyService {

    @Override
    public void addBusinessProperty(BusinessProperty businessProperty) {
        if (StringUtils.isBlank(businessProperty.getName())) {
            throw new ServiceException("属性名称不能为空");
        }
        if (StringUtils.isBlank(businessProperty.getCode())) {
            throw new ServiceException("属性编码不能为空");
        }
        if (StringUtils.isBlank(businessProperty.getType())) {
            throw new ServiceException("属性类型不能为空");
        }

        List<BusinessProperty> properties = this.list(
                new QueryWrapper<BusinessProperty>()
                        .eq(BusinessProperty.COL_BUSINESS_RULE_TEMP_ID, businessProperty.getBusinessRuleTempId())
                        .eq(BusinessProperty.COL_CODE, businessProperty.getCode())
        );
        if (CollectionUtils.isNotEmpty(properties)) {
            throw new ServiceException("相同业务下, 属性编码不能重复");
        }
        this.save(businessProperty);
    }
}
